package com.cheyunkeji.er.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.cheyun.netsalev3.R;


public class TabTitle extends LinearLayout implements View.OnClickListener {
    View contentView;
    ScrollView targetSV;
    TextView tvTitle;
    ImageView ivArr;
    //是否展开
    boolean isExpand = true;
    private Context context;
    private LayoutInflater mInflater;
    private View mTabTitle;
    private ImageView ivIndicator;

    public TabTitle(Context context) {
        super(context);
        this.context = context;
        init(context);
    }

    public TabTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(context);
    }

    private void init(Context context) {
        mInflater = LayoutInflater.from(context);
        mTabTitle = mInflater.inflate(R.layout.er_v_tabtitle, null);
        addView(mTabTitle);

        tvTitle = (TextView) findViewById(R.id.tvTitle);
        ivArr = (ImageView) findViewById(R.id.ivArr);
        ivIndicator = (ImageView) mTabTitle.findViewById(R.id.iv_indicator);
        mTabTitle.setOnClickListener(this);
    }

    public void setTitle(String text) {
        tvTitle.setText(text);
    }

    public void setView(View v) {
        contentView = v;
    }

    public void setView(View v, ScrollView sv) {
        contentView = v;
        targetSV = sv;
    }

    public void setExpand(boolean isExpand) {
        this.isExpand = isExpand;
        setExpand();
    }

    /**
     * 隐藏左侧图标
     */
    public void hideIndicator() {
        ivIndicator.setVisibility(View.GONE);
    }

    private void setExpand() {
        if (isExpand) {
            ivArr.setImageResource(R.mipmap.er_ic_arr_up);
            if (contentView != null && contentView.getVisibility() != VISIBLE) {
//                contentView.setVisibility(View.VISIBLE);
                expand(contentView);
                if (targetSV != null) {
                    targetSV.post(new Runnable() {
                        @Override
                        public void run() {
                            int[] location = new int[2];
                            contentView.getLocationOnScreen(location);

                            int offset = location[1] - targetSV.getMeasuredHeight();
                            //+ contentView.getMeasuredHeight() / 2;
                            if (offset > 0) {
                                targetSV.smoothScrollBy(0, offset);
                            }
                        }
                    });
                }
            }
        } else {
            ivArr.setImageResource(R.mipmap.er_ic_arr_down);
            if (contentView != null && contentView.getVisibility() != GONE) {
//                contentView.setVisibility(View.GONE);
                collapse(contentView);
            }
        }
    }

    private Animation animation;

    private void collapse(final View v)
    {
        final int initialHeight = v.getMeasuredHeight();
        animation = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if(interpolatedTime == 1)
                {
                    v.setVisibility(View.GONE);
                }
                else{
                    v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        animation.setDuration(200);
        v.startAnimation(animation);
    }

    private void expand(final View v)
    {
        v.measure(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();
        v.getLayoutParams().height = 0;
        v.setVisibility(VISIBLE);

        animation = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t)
            {
                if (interpolatedTime == 1)
                v.getLayoutParams().height = (interpolatedTime == 1) ? RelativeLayout.LayoutParams.WRAP_CONTENT : (int) (targetHeight * interpolatedTime);
                v.requestLayout();
            }


            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        animation.setDuration(200);
        v.startAnimation(animation);
    }


    @Override
    public void onClick(View view) {
        if (view == mTabTitle) {
//            if (!isExpand) {
//                isExpand = true;
//                setExpand();
//            } else {
//                isExpand = false;
//                setExpand();
//            }
            isExpand = !isExpand;
            setExpand();
        }
    }
}
